﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XCommunity.Frames
{
  public static class Extensions
  {
    /// <summary>
    /// 最后一个发布的帖子
    /// </summary>
    public static IPostFrame GetLastPublishPost( this ITopicFrame topic )
    {
      IPostFrame post = GetLastPublishResponse( topic );
      if ( post != null )
        return post;
      else
        return topic.Theme;
    }


    /// <summary>
    /// 最后一个发布的回复
    /// </summary>
    public static IPostFrame GetLastPublishResponse( this ITopicFrame topic )
    {
      if ( topic.Responses.Any() )
        return topic.Responses.Aggregate( ( post1, post2 ) => post1.PublishDate > post2.PublishDate ? post1 : post2 );
      else
        return null;
    }



    /// <summary>
    /// 最后一个修改的帖子
    /// </summary>
    public static IPostFrame GetLastModifyPost( this ITopicFrame topic )
    {
      return topic.Responses.Aggregate( topic.Theme, ( post1, post2 ) => post1.LastModifyDate > post2.LastModifyDate ? post1 : post2 );
    }


    /// <summary>
    /// 最后一个修改的回复
    /// </summary>
    public static IPostFrame GetLastModifyResponse( this ITopicFrame topic )
    {
      if ( topic.Responses.Any() )
        return topic.Responses.Aggregate( ( post1, post2 ) => post1.LastModifyDate > post2.LastModifyDate ? post1 : post2 );
      else
        return null;
    }

  }
}
